<template>
  <div class="min">
    <div class="head">{{$t('nav.position')}}</div>
    <van-row type="flex" class="head-label" justify="space-between" align="center">
      <van-col>{{ $t('asset_name') }}</van-col>
      <van-col>{{ $t('price_') }}</van-col>
      <van-col>{{ $t('up_down') }}</van-col>
    </van-row>
    <div class="list">
      <template v-if="robotData.length">
        <van-swipe-cell v-for="item in robotData" :key="item.id" class="ticker-item" disabled>
          <van-cell v-if="item.status == 1 && item.values_str !==undefined" @click='goDetail(item)'>
            <van-row type="flex" justify="space-between" align="center">
              <van-col>
                <div class="stock">{{ item.stock }}<i>/{{ item.money}}</i></div>
                <div class="price">{{ getDeal(item.values_str,'deal_amount') | numberFormat(6)}}（{{ item.platform }}）</div>
              </van-col>
              <van-col class="col-right center">
                <div class="first_order_value">{{ getDeal(item.values_str,'base_price') | numberFormat(6)}}</div>
                <div class="revenue">${{ (getDeal(item.values_str,'deal_money') /getDeal(item.values_str,'deal_amount')) |numberFormat(6)}}</div>
              </van-col>
              <van-col class="col-right">
                <div class="button" :class=" item.rate < 0 ? 'hq-red' : 'hq-green'">{{item.rate < 0 ? '' : '+'}}{{  item.rate*100 | numberFormat(2)}}%</div>
                <div class="floatPrice" :class=" item.rate < 0 ? 'hq-red' : 'hq-green'">${{item.rate < 0 ? '' : '+'}}{{  Number(item.revenue) | numberFormat(6) }}</div>
              </van-col>
            </van-row>
          </van-cell>
        </van-swipe-cell>
      </template>
      <van-empty v-else :description="$t('empty.default')" />
    </div>
    <ticker-drawer ref="drawer" />
  </div>
</template>

<script>
import { SwipeCell } from 'vant'
import { mapState, mapActions } from 'vuex'
import TickerDrawer from '../ticker/components/TickerDrawer'
let timer = null
let isDestroy = false
export default {
  layout: 'navigation',
  components: {
    [SwipeCell.name]: SwipeCell,
    TickerDrawer
  },
  data () {
    return {
      isLoading: false
    }
  },
  computed: {
    ...mapState({
      tickerList: ({ ticker }) => ticker.list,
      robotData: ({ robot }) =>  robot.robotList
    })
  },
  async created () {
    isDestroy = false
    await this.robotList();
    const timer = setInterval(async () => {
        await this.robotList()
      }, 4000);
     this.$once('hook:beforeDestroy', () => {
      clearTimeout(timer)
    })

    // this.$once('hook:beforeDestroy', () => {
    //   isDestroy = true
    // })
  },
  methods: {
    ...mapActions({
    robotList: 'robot/robotList'
    }),
    handleLink (path) {
      this.$router.push(path)
    },
    getDeal (values,id) {
      if (values) {
        const valueJson = JSON.parse(values)
        return Number(valueJson[id]) || '-'
      }
      return '-'
    },
    goDetail (item) {
      this.$nextTick(() => {

        if (item.type == 2) {
          this.$router.push({
            name: 'robot-tract',
            query: { market_id: item.market_id }
          })
        } else {
          this.$router.push({
            name: 'robot',
            query: { market_id: item.market_id }
          })
        }

      })
    },
    getRate({price,first_order_value}){

      // alert(first_order_value)
        let val  = ((price - first_order_value) / first_order_value).toFixed(5)
        return {
            str:val < 0 ? '' : '+',
            val
        }
    }
  }
}
</script>

<style scoped lang="less">
.min{
  padding: 0 11px;
  .head{
    padding: 14px 0 20px 0;
    font-size: 20px;
    text-align: center;
    font-weight: 500;
    color: #000000;
  }
  .head-label{
    padding: 0 12px;
    font-size: 13px;
    font-weight: 400;
    color: #000000;
  }

  .list{
    margin-top: 14px;
    .van-cell{
      padding: 0 12px;
      height: 94px;
      background: #FFFFFF;
      border-radius: 12px;
      margin-bottom: 8px;
      &:after{
        display: none;
      }
      .van-cell__value {
        display: flex;
        align-items: center;
        .van-row{
          width: 100%;
          .van-col{
            flex: 1;
            line-height: 1;
            &:first-child{
              flex: 120px 0 0;
              width: 120px;
            }
            &.col-right{
              text-align: right;
            }
            &.center{
              box-sizing: content-box;
            }
            .stock{
              font-size: 20px;
              font-weight: bold;
              color: #F5A623;
              i{
                font-size: 13px;
                font-weight: 400;
                color: #3871F9;
                font-style: normal;
              }
            }
            .price{
              margin-top: 10px;
              font-size: 13px;
              font-weight: 400;
              color: #727383;
            }
            .first_order_value{
              padding-right: 10px;
              font-size: 18px;
              line-height: 20px;
              font-weight: bold;
              color: #3871F9;
            }
            .revenue{
              padding-right: 10px;
              margin-top: 10px;
              font-size: 13px;
              font-weight: 400;
              color: #727383;
            }
            .button{
              padding: 0 10px;
              height: 28px;
              line-height: 28px;
              display: inline-block;
              text-align: center;
              font-size: 13px;
              font-weight: 500;
              color: #FFFFFF;
              border-radius: 15px;
              &.hq-red{
                background: #FF5D5D;
              }
              &.hq-green{
                background: #23D7AF;
              }
            }
            .floatPrice{
              margin-top: 5px;
              font-size: 13px;
              font-weight: 500;
              &.hq-red{
                color: #FF5D5D;
              }
              &.hq-green{
                color: #23D7AF;
              }
            }
          }
        }
      }
    }
  }
}
</style>
